<template>
  <text>{{ coutDownTimeChina }}</text>
</template>

<script>
const util = require("@/util/util.js");
export default {
  props: {
    setTime: {
      type: Number,
      value: 0,
    },
    color: {
      default: "",
      type: String,
    },
    fontSize: {
      default: "",
      type: String,
    },
  },
  data() {
    return {
      coutDownTime: new Date().getTime() + 24 * 60 * 60 * 1000,
      coutDownTimeChina: "",
      coutTimer: "",
    };
  },
  methods: {
    formatDuring(mss) {
      var days = parseInt(mss / (1000 * 60 * 60 * 24));
      days = days > 0 ? days + "天" : "";
      var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      hours = `${hours < 10 ? "0" : ""}${hours < 0 ? 0 : hours}时`;
      var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
      minutes = `${minutes < 10 ? "0" : ""}${minutes < 0 ? 0 : minutes}分`;
      var seconds = parseInt((mss % (1000 * 60)) / 1000);
      seconds = `${seconds < 10 ? "0" : ""}${seconds < 0 ? 0 : seconds}秒`;
      return `${days}${hours}${minutes}${seconds}`;
    },
    startCout() {
      let tempTime = parseInt(this.coutDownTime) - new Date().getTime();
      let nextTime = new Date().getTime() + 24 * 60 * 60 * 1000;
      if (tempTime <= 0 || tempTime > nextTime) {
        tempTime = 24 * 60 * 60 * 1000;
        this.coutDownTime = nextTime;
        this.$emit('countFinish');
      }
      this.coutDownTimeChina = this.formatDuring(tempTime);
    },
    startCoutTimer() {
      this.startCout();
      this.coutTimer = setInterval(() => {
        this.startCout();
      }, 1000);
    },
  },
  onReady() {},
  async onShow(e) {},
  async created() {
    console.log(this.setTime,'setTime');
    if (this.setTime !== 0) {
      this.coutDownTime = this.setTime;
      this.startCoutTimer();
      return;
    }
    if (uni.getStorageSync("coutDownTime")) {
      this.coutDownTime = uni.getStorageSync("coutDownTime");
    }
    this.startCoutTimer();
  },
  destroyed() {
    if (this.setTime === 0)
      uni.setStorageSync("coutDownTime", this.coutDownTime);
    clearInterval(this.coutTimer);
    clearInterval(this.timer);
  },
};
</script>

<style lang="scss" scoped>
</style>
